#include <fstream.h>
#include <iostream>

#include "forest.h"

extern unsigned int UniqId;

/* ************************************************************
 *                                                            *
 ************************************************************ */
forest::forest (string cat, class features sem*)
{
  Cat=cat;
  Sem=sem;
  ForestId=UniqId++;
  Seen=false;
}; 

/* ************************************************************
 *                                                            *
 ************************************************************ */
class node *
forest::addNode(string span)
{
  class node *n=new node(span);
  Nodes.push_back(n);
  return n;
}

/* ************************************************************
 *                                                            *
 ************************************************************ */
void
forest::notSeen()
{
  Seen=false;
  list<class node *>::const_iterator it;
  for(it=Nodes.begin(); it!=Nodes.end(); it++){
    (*it)->notSeen();
  } 
}

/* ************************************************************
 *                                                            *
 ************************************************************ */
void
forest::print(ostream& out_stream)
{
  if (Seen)
    return;
  Seen=true;
  out_stream << "F" << ForestId << " [shape=\"egg\", label=\"" << Cat << "\\n" ;
  if(Sem)
    Sem->print(out_stream);
  out_stream << "\"";
  out_stream << "];" << endl;
  for(list<class node *>::const_iterator i=Nodes.begin() ; i !=Nodes.end() ; i++){
    if (!(*i)->Seen){
      (*i)->Seen=true;
      out_stream << "N" << (*i)->NodeId << endl;
      out_stream << "F" << ForestId << " -> N" << (*i)->NodeId << endl;
      int j=1;
      for (vector<class forest *>::const_iterator s=(*i)->Sons.begin() ; s!=(*i)->Sons.end() ; s++){
	char n[32];
	sprintf(n, "%d", j++);
      	out_stream << "N" << (*i)->NodeId << " -> F" << (*s)->ForestId << "[label=\"" << n << "\"]" << endl;
	(*s)->print(out_stream);
      }
    }
  }
}

/* ************************************************************
 *                                                            *
 ************************************************************ */
void
forest::eval(string tab)
{
}

